// pages/hdetails/hdetails.js
const app = getApp()
const net = app.globalData.net;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    hinfo: {},
    userinfo: {},
    uid: 0,
    rid: 0,
    hid: 0,
    idcard: 0,
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    price: 0,
    furoth: false,
    furup: true,
    furniture: [{
        imgurl: '../imgs/vrv.png',
        furname: '空调',
      },
      {
        imgurl: '../imgs/tv.png',
        furname: '电视',
      },
      {
        imgurl: '../imgs/bx.png',
        furname: '冰箱',
      },
    ],
    entsta: false,
    wsmes: [],
    zhz: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("你现在到了详情页")
    wx.getStorage({
      key: 'uid',
      success: (res) => {
        this.setData({
          uid: res.data,
        });
        wx.request({
          url: net + "/user/getuserbyId",
          method: "POST",
          data: {
            uid: res.data,
          },
          success: (res) => {
            console.log("this.data.userinfo", res.data.data)
            this.setData({
              userinfo: res.data.data,
            })
          }
        })
      },
    })
    
    // 接收列表页传过来的hid
    this.setData({
      _num: 2
    })
    let r = options.hid
    // 根据hid向服务器请求房间信息数据
    wx.request({
      url: net + '/house/listone',
      method: 'POST',
      data: {
        hid: r
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log('详情页res', res.data.data)

        let _arr = []
        let imgurl = []
        for (let v of res.data.data.rooms) {
          _arr.push(v);
        }
        if (res.data.data.h_img) {
          imgurl = res.data.data.h_img.split(",");
        }
        if (res.data.data.user) {
          app.globalData.uid = res.data.data.user.uid;
        }
        if (res.data.data.rooms) {
          this.setData({
            rid: res.data.data.rooms.roomid,
          })
        }
        this.setData({
          price: res.data.data.z_pay,
          wsmes: _arr,
          hzsta: res.data.data.you_can,
          hinfo: res.data.data,
          imgUrls: imgurl,

        })

      },
    });
    this.setData({
      _num: 2
    });
    this.setData({
      entsta: true,
    });
  },
  hienhos1(e) {
    this.setData({
      price: this.data.hinfo.z_pay
    })
    if (!this.data.zhz) {
      this.setData({
        zhz: 1,
        entsta: true,
        hid: this.data.hinfo.hid,
      })
    } else {
      this.setData({
        zhz: 0,
      })
    }

  },
  hienhos(e) {
    if (this.data.zhz != 2) {
      this.setData({
        zhz: 2
      })
    } else {
      this.setData({
        zhz: 0
      })
    }
    if (!this.data.hinfo.you_can) {
      if (!this.data.entsta) {
        this.setData({
          entsta: true,
        })
      } else {
        this.setData({
          entsta: false,
        })
      }
    }

  },
  hipri(event) {
    //选择房间
    let num = event.currentTarget.dataset.num;
    let max = event.currentTarget.dataset.max;
    let _rid = event.currentTarget.dataset.rid;
    if (_rid) {
      this.setData({
        rid: _rid
      })
    }
    if (num < max) {
      this.setData({
        price: event.currentTarget.dataset.pri
      })
    } else if (num === max) {
      wx.showToast({
        title: '该房间已住满',
        image: '../imgs/cry.png'
      })
    }
  },
  otherfur() {
    //房间配置
    if (!this.data.furoth) {
      this.setData({
        furniture: [{
            imgurl: '../imgs/vrv.png',
            furname: '空调',
          },
          {
            imgurl: '../imgs/tv.png',
            furname: '电视',
          },
          {
            imgurl: '../imgs/bx.png',
            furname: '冰箱',
          },
          {
            imgurl: '../imgs/Wifi.png',
            furname: '宽带',
          },
          {
            imgurl: '../imgs/xyj.png',
            furname: '洗衣机',
          },
          {
            imgurl: '../imgs/sf.png',
            furname: '沙发',
          },
          {
            imgurl: '../imgs/bed.png',
            furname: '床',
          },
          {
            imgurl: '../imgs/wc.png',
            furname: '卫生间',
          },
        ],
        furoth: true,
        furup: false,
      })
    } else if (!this.data.furup) {
      let fuarr = [];
      let _temarr = this.data.furniture.splice(0, 3);
      this.setData({
        furniture: _temarr,
        furoth: false,
        furup: true,
      })
    }
  },
  toonlin() {
    //立即预定
    let rid = this.data.rid;
    console.log("详情页rid", rid);
    // 生成订单
    wx.request({
      url: net + '/user/addorder',
      method: 'POST',
      data: {
        hid: this.data.hinfo.hid,
        roomid: rid,
        uid: this.data.uid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log("请求订单接口:", res);
      },
    }); 
    if (!this.data.uid) {
      console.log("判断用户是否登录时的uid", this.data.uid);
      // 判断用户是否登录
      wx.showModal({
        title: '您还没有登录哦',
        showCancel: true,
        confirmText: '去登陆',
        success: (res) => {
          if (res.confirm) {
            wx.reLaunch({
              url: `/pages/logNum/logNum?hid=${this.data.hinfo.hid}`,
            })
          }
        }
      })
    } else if (!this.data.userinfo.idcard) {
      console.log("userinfo2", this.data.userinfo)
      wx.showModal({
        title: '您还没有实名认证哦',
        showCancel: true,
        confirmText: '马上实名',
        success: (res) => {
          if (res.confirm) {
            wx.reLaunch({
              url: `/pages/home/logmessage/logmessage?hid=${this.data.hinfo.hid}`,
            })
          }
        }
      })
    } else if (this.data.hid || this.data.rid) {
      //判断用户是否选择了房间,选择了跳转至签约合同页面
      wx.navigateTo({
        url: `/pages/onlinesigning/onlinesigning?hid=${this.data.hinfo.hid}&rid=${rid}&uid=${app.globalData.uid}`,
      })
    } else {
      wx.showToast({
        title: '请选择房间',
        image: '/pages/home/imgs/cry.png',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})